// pages/myself/balance/balance.js
var Zan = require('../../../dist/index.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;

Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    account: [],
    radioCheckVal: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    /**
         * 获取个人及商家信息
         */
    if (app.globalData.userInfo) {
      this.setData({
        balance: app.globalData.userInfo.balance
      })
    } else {
      app.userInfoCallback = res => {
        this.setData({
          balance: res.balance
        })
      }
    }
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("RechargeRefreshNotification", this.refreshNotification, this);
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: "余额",
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("RechargeRefreshNotification", this);
  },
  refreshNotification:function(){
    this.onPullDownRefresh();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      account: [],
    });
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId }, getUserInfosuccess);
    network._post_page_json(this, api.getBills, start, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.account.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.getBills, start, {}, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  // balanceDetails: function (e) {
  //   var type = e.currentTarget.dataset.type;
  //   var id = e.currentTarget.dataset.id;
  //   if (type == 0) {
  //     wx.navigateTo({
  //       url: '/pages/myself/balance/RefundDetails?id=' + id
  //     })
  //   } else {
  //     wx.navigateTo({
  //       url: '/pages/myself/balance/BalanceDetails?id=' + id
  //     })
  //   }
  // },

  openMember: function () {
    wx.navigateTo({
      url: '/pages/myself/balance/Recharge',
    })
  }
}))
var getUserInfosuccess = function (that, res) {
  that.setData({
    balance: res.data.data.balance
  })
}
// 获取明细列表结果
var success = function (that, res) {

  if (res.data.data) {
    for (let a = 0; a < res.data.data.length; a++) {
      res.data.data[a].money = parseInt(res.data.data[a].money);
      res.data.data[a].returnMoney = parseInt(res.data.data[a].returnMoney);
    }
    that.setData({
      account: that.data.account.concat(res.data.data),
      recordsTotal: res.data.count
    })
  }
}
